﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Windows.System.Threading;
using InfoReaderLib.Parser;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using InfoReaderLib.Info;
using System.Runtime.InteropServices.WindowsRuntime;

namespace InfoReaderLib.Tester
{
    [TestClass]
    public class SimpleParserTest
    {
        //[TestMethod]
        public async Task TestMethod1()
        {
            IParser parser = new Parser.SimpleParser.Parser();

            // inject info source
            var infoSource = new InfoReaderLib.Info.InfoSource();
            infoSource.Url = "http://welovewp.hk/";
            infoSource.ParserName = "SimpleParser";
            parser.DataSource = infoSource;

            // inject setting
            string json = await Util.FillJSONObjectFromAnotherFileAsync(@"Data\SimpleParserSettingJSON.txt",
                new Dictionary<string, string>(){
                        {"ListRegEx", @"Data\SimpleParserRegExList.txt"}
                    });
            Assert.IsTrue(json != null && json.Length > 0);

            parser.SettingJSON = json;
            IInfo info = await parser.Parse();

            Assert.IsTrue(info != null);

            var infoList = (IInfoList)info;
            Assert.IsTrue(infoList.List != null);
            Assert.IsTrue(infoList.List.Count > 0);

            Util.Debug("Title=" + infoList.Title + ", Return Count=" + infoList.List.Count);
            foreach (InfoDetail listItem in infoList.List)
            {
                Util.Debug("Item Author=" + listItem.Author);
                Util.Debug("Item CreateDateText=" + listItem.CreateDateText);
                Util.Debug("Item Title=" + listItem.Title);
                Util.Debug("Item Url=" + listItem.Url);
            }
        }

        //[TestMethod]
        public async Task TestReader()
        {
            // initialize info source
            var infoSource = new InfoReaderLib.Info.InfoSource();
            infoSource.Url = "http://welovewp.hk/";
            infoSource.ParserName = "SimpleParser";

            // obtain setting
            string json = await Util.FillJSONObjectFromAnotherFileAsync(@"Data\SimpleParserSettingJSON.txt",
                new Dictionary<string, string>(){
                        {"ListRegEx", @"Data\SimpleParserRegExList.txt"}
                    });
            Assert.IsTrue(json != null && json.Length > 0);

            // initialize reader
            Reader reader = new Reader()
            {
                Source = infoSource,
                ParserSetting = json
            };
            reader.Init();

            // test the reader
            IInfo info = await reader.Parse();

            Assert.IsTrue(info != null);

            var infoList = (IInfoList)info;
            Assert.IsTrue(infoList.List != null);
            Assert.IsTrue(infoList.List.Count > 0);

            Util.Debug("Title=" + infoList.Title + ", Return Count=" + infoList.List.Count);
            foreach (InfoDetail listItem in infoList.List)
            {
                Util.Debug("Item Author=" + listItem.Author);
                Util.Debug("Item CreateDateText=" + listItem.CreateDateText);
                Util.Debug("Item Title=" + listItem.Title);
                Util.Debug("Item Url=" + listItem.Url);
            }
        }

        //[TestMethod]
        public void TestSync()
        {
            AsyncInfo.Run(async _ =>
            {
                Util.Debug("start");
                string json = await Util.FillJSONObjectFromAnotherFileAsync(@"Data\SimpleParserSettingJSON.txt",
                    new Dictionary<string, string>(){
                        {"ListRegEx", @"Data\SimpleParserRegExList.txt"}
                    });
                Util.Debug(json);
            });
        }

        [TestMethod]
        public async Task TestReaderDetail()
        {
            Util.DebugTime();
            // initialize info source
            var infoSource = new InfoReaderLib.Info.InfoSource();
            infoSource.Url = "http://welovewp.hk/";
            infoSource.ParserName = "SimpleParser";

            // obtain setting
            string json = await Util.FillJSONObjectFromAnotherFileAsync(@"Data\SimpleParserSettingJSON.txt",
                new Dictionary<string, string>(){
                    {"ListRegEx", @"Data\SimpleParserRegExList.txt"},
                    {"DetailRegEx", @"Data\SimpleParserRegExDetail.txt"}
                });
            Assert.IsTrue(json != null && json.Length > 0);

            // initialize reader
            Reader reader = new Reader()
            {
                Source = infoSource,
                ParserSetting = json
            };
            reader.Init();
            Util.DebugTime();

            // test the reader
            IInfo info = await reader.Parse();

            Assert.IsTrue(info != null);

            var infoList = (IInfoList)info;
            Assert.IsTrue(infoList.List != null);
            Assert.IsTrue(infoList.List.Count > 0);

            // fill in form
            InfoDetail infoDetail = null;
            Util.Debug("Title=" + infoList.Title + ", Return Count=" + infoList.List.Count);
            foreach (InfoDetail listItem in infoList.List)
            {
                Util.Debug("Item Author=" + listItem.Author);
                Util.Debug("Item CreateDateText=" + listItem.CreateDateText);
                Util.Debug("Item Title=" + listItem.Title);
                Util.Debug("Item Url=" + listItem.Url);

                if (infoDetail == null)
                    infoDetail = listItem;
            }
            Util.DebugTime();

            /// onLoad calls this
            if (infoDetail != null)
            {
                IInfo result = await reader.Parse(infoDetail);
                Util.Debug("first result article");
                Util.Debug("Item Author=" + infoDetail.Author);
                Util.Debug("Item CreateDateText=" + infoDetail.CreateDateText);
                Util.Debug("Item Title=" + infoDetail.Title);
                Util.Debug(((IInfoDetail)result).Article);
            }
            Util.DebugTime();
        }
    }
}
